package com.banyuan.servlet.types;

import com.alibaba.fastjson.JSON;
import com.banyuan.dao.TypesDao;
import com.banyuan.domain.ResData;
import com.banyuan.domain.Types;
import com.banyuan.utils.AuthorUtils;
import com.banyuan.utils.CommonUtils;
import com.banyuan.utils.DBUtils;
import com.banyuan.utils.JDBCUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/types/update")
public class UpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String intro = req.getParameter("intro");
        String name = req.getParameter("name");

        // 1.判断参数
        if (CommonUtils.isEmpty(id) || CommonUtils.isEmpty(intro) ||
                CommonUtils.isEmpty(name) ) {
            ResData resData = new ResData(10001,"输入项为空",null);
            resp.getWriter().write(JSON.toJSONString(resData));
            return; //阻断下面代码的运行
        }

        // 2.判断传进来的name和数据库里的name是否一样
        // ，一样就先放行，不一样的，去查询是否重复，重复的话就return
        ResultSet resultSet = null;
        ResultSet rs = null;
        Types types = new Types();
        try {
            resultSet = TypesDao.selectTypesById(Integer.parseInt(id));
            types = DBUtils.resultToBeanOne(resultSet, Types.class);
            if (!name.equals(types.getName())) {
                 rs = TypesDao.selectByName(name);
                 rs.next();
                int countSum = rs.getInt("countSum");

                if (countSum > 0) {
                    // 如果统计出来大于0 就是名字重复了
                    ResData resData = new ResData(10002,"分类名称重复了，请换一个",null);
                    resp.getWriter().write(JSON.toJSONString(resData));
                    return; //阻断下面代码的运行
                }
            }


            // 3.都没问题的话，就修改到数据库里
            types.setName(name.trim()); //去掉空格
            types.setOperator(AuthorUtils.getLoginUserName(req));
            types.setIntro(intro.trim()); //

            int update = TypesDao.update(types);

            if (update > 0) {
                ResData resData = new ResData(200,"修改成功",null);
                resp.getWriter().write(JSON.toJSONString(resData));
            } else {
                ResData resData = new ResData(10003,"修改失败，请稍后再试",null);
                resp.getWriter().write(JSON.toJSONString(resData));
            }


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeBy(rs);
            JDBCUtils.closeBy(resultSet);
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
